/**
 * @file inOutCtrl.c
 * @brief
 * @author  xiaowine (xiaowine@sina.cn)
 * @version 01.00
 * @date    2020-12-10
 *
 * @copyright Copyright (c) {2020}  xiaowine
 *
 * @par 修改日志:
 * <table>
 * <tr><th>Date       <th>Version <th>Author  <th>Description
 * <tr><td>2020-12-10 <td>1.0     <td>wangh     <td>内容
 * </table>
 * ******************************************************************
 * *                   .::::
 * *                 .::::::::
 * *                ::::::::::
 * *             ..:::::::::::
 * *          '::::::::::::
 * *            .:::::::::
 * *       '::::::::::::::..        女神助攻,流量冲天
 * *            ..::::::::::::.     永不宕机,代码无bug
 * *          ``:::::::::::::::
 * *           ::::``:::::::::'        .:::
 * *          ::::'   ':::::'       .::::::::
 * *        .::::'      ::::     .:::::::'::::
 * *       .:::'       :::::  .:::::::::' ':::::
 * *      .::'        :::::.:::::::::'      ':::::
 * *     .::'         ::::::::::::::'         ``::::
 * * ...:::           ::::::::::::'              ``::
 * *```` ':.          ':::::::::'                  ::::.
 * *                   '.:::::'                    ':'````.
 * ******************************************************************
 */

#include "inOutCtrl.h"
#include "opration.h"

void inOutCtrlInit(void)
{
    GPIO_InitTypeDef GPIO_InitStructure;

    /* 第1步：打开GPIO时钟 */
    RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_GPIOB | RCC_APB2Periph_GPIOC | RCC_APB2Periph_GPIOE | RCC_APB2Periph_GPIOF | RCC_APB2Periph_GPIOG, ENABLE);

    /* 第2步：配置所有的按键GPIO为浮动输入模式(实际上CPU复位后就是输入状态) */
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
    GPIO_InitStructure.GPIO_Mode  = GPIO_Mode_IN_FLOATING; /* 输入浮空模式 */

    // GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
    // GPIO_Init(GPIOC, &GPIO_InitStructure);
    /* 第2步：配置所有的按键GPIO为浮动输入模式(实际上CPU复位后就是输入状态) */
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
    GPIO_InitStructure.GPIO_Mode  = GPIO_Mode_Out_PP; /* 推挽输出 */

    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_5 | GPIO_Pin_6 | GPIO_Pin_8 | GPIO_Pin_9 | GPIO_Pin_10 | GPIO_Pin_11;
    GPIO_Init(GPIOA, &GPIO_InitStructure);
    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5 | GPIO_Pin_8 | GPIO_Pin_9;
    GPIO_Init(GPIOB, &GPIO_InitStructure);

    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8 | GPIO_Pin_9;
    GPIO_Init(GPIOC, &GPIO_InitStructure);

    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0;
    GPIO_Init(GPIOD, &GPIO_InitStructure);

    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0;
    GPIO_Init(GPIOE, &GPIO_InitStructure);

    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4 | GPIO_Pin_5 | GPIO_Pin_8 | GPIO_Pin_12 | GPIO_Pin_13 | GPIO_Pin_14 | GPIO_Pin_15;
    GPIO_Init(GPIOG, &GPIO_InitStructure);

    PDout(0) = outPutOff;

    PEout(0)  = outPutOff;
    PBout(9)  = outPutOff;
    PBout(8)  = outPutOff;
    PBout(5)  = outPutOff;
    PGout(15) = outPutOff;
    PGout(14) = outPutOff;
    PGout(13) = outPutOff;
    PGout(12) = outPutOff;
    PAout(11) = outPutOff;
    PAout(10) = outPutOff;
    PAout(9)  = outPutOff;
    PAout(8)  = outPutOff;
}

void inOutCtrl(void)
{
    if (optData.data1 != 1)
        PEout(0) = outPutOff;
    if (optData.data1 != 2)
        PBout(9) = outPutOff;
    if (optData.data1 != 3)
        PBout(8) = outPutOff;
    if (optData.data1 != 4)
        PBout(5) = outPutOff;
    if (optData.data1 != 5)
        PGout(15) = outPutOff;
    if (optData.data1 != 6)
        PGout(14) = outPutOff;
    if (optData.data1 != 7)
        PGout(13) = outPutOff;
    if (optData.data1 != 8)
        PGout(12) = outPutOff;
    if (optData.data1 != 9)
        PAout(11) = outPutOff;
    // if (optData.data1 != 10)
    //     PAout(10) = outPutOff;
    // if (optData.data1 != 11)
    //     PAout(9) = outPutOff;
    if (optData.data1 != 12)
        PAout(8) = outPutOff;

    if (warnFlag == 0) {
        if (optData.data0 == 0) {
        } else if ((optData.data0 == 1) && (optData.data1 != 1)) {
            PEout(0) = outPutOn;
        } else if ((optData.data0 == 2) && (optData.data1 != 2)) {
            PBout(9) = outPutOn;
        } else if ((optData.data0 == 3) && (optData.data1 != 3)) {
            PBout(8) = outPutOn;
        } else if ((optData.data0 == 4) && (optData.data1 != 4)) {
            PBout(5) = outPutOn;
        } else if ((optData.data0 == 5) && (optData.data1 != 5)) {
            PGout(15) = outPutOn;
        } else if ((optData.data0 == 6) && (optData.data1 != 6)) {
            PGout(14) = outPutOn;
        } else if ((optData.data0 == 7) && (optData.data1 != 7)) {
            PGout(13) = outPutOn;
        } else if ((optData.data0 == 8) && (optData.data1 != 8)) {
            PGout(12) = outPutOn;
        } else if ((optData.data0 == 9) && (optData.data1 != 9)) {
            PAout(11) = outPutOn;
        } else if ((optData.data0 == 10) && (optData.data1 != 10)) {
            // PAout(10) = outPutOn;
        } else if ((optData.data0 == 11) && (optData.data1 != 11)) {
            // PAout(9) = outPutOn;
        } else if ((optData.data0 == 12) && (optData.data1 != 12)) {
            PAout(8) = outPutOn;
        }
    }

    /******************************************/
    if (HIMKEY0) {  // 点动
        PGout(5) = outPutOn;
    } else {
        PGout(5) = outPutOff;
    }
    if (HIMKEY1) {  // 保持
        PGout(4) = outPutOn;
    } else {
        PGout(4) = outPutOff;
    }
}

void pc9Ctrl(void)
{
    static uint16_t count = 0;
    if (optData.data2) {
        PCout(9) = outPutOn;  // 熄灭
    } else {
        uint16_t sheld = optData.pc9Period / 2;  // 亮的时间长度
        if (count < sheld) {
            count++;
        } else if (count == sheld) {
            count++;
            PCout(9) = outPutOn;  // 熄灭
        } else if (count < optData.pc9Period) {
            count++;
        } else {
            PCout(9) = outPutOff;  // 点亮
            count    = 0;
        }
    }
}
